// pages/releaseRecruitment/releaseRecruitment.js
var that,
  app = getApp(),
  api = new (require('../../utils/api.js')),
  util = new (require('../../utils/util.js')),
  headimg = api.getimgurl();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgurl:[],
    isagree:!0,
    gettopindex:0,
    industry_choseindex:0,
    industrylist:['请选择','酒店餐饮','建筑家装','休闲娱乐','零售百货','家政保安','汽车服务','教育培训','广告传媒','金融保险','医疗保健','矿场企业','物流运输','其他'],
    quicklist: ['五险一金','交通补助','交通补助','餐费补助','五险一金','有证优先', '包吃住', '急招', '宿舍有空调'],
    gettoplist:util.gettoplist(),
    worktypelist: [[], [{ name: '全部', id: 0 }]],
    primarytypeList: [],//一级工种分类列表
    subtypeList: [],//二级工种分类列表
    worktype: 0,//一级工种id
    worksubtype: 0,//二级工种id
    worktypename: '',//一级工种
    worksubtypename: '',//二级工种
    detail: '',//招工详情
    contacts: '',//联系人
    phone: '1333333333333',//'',//联系电话
    recruitsnum: '',//招聘人数
    salary: '',//薪资/价格
    title: '',//标题
    region:['请选择地区'],
    province: '',
    city: '',
    district: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    that.getdata()
  },
  getdata() {
    // wx.showLoading({
    //   title: '加载中...',
    // })
    let { primarytypeList, subtypeList, worktypelist } = that.data, newslist = [{ name: '全部', id: 0 }]
    // api.getsubMechanicstypelist(res => {
    //   wx.hideLoading()
    //   if (res.data.issuccess == 1) {
    //     primarytypeList = newslist.concat(res.data.list)
    //     subtypeList = res.data.sublist
    //     worktypelist[0] = primarytypeList
    //     that.setData({ worktypelist, subtypeList, primarytypeList })
    //   }
    // })
  },
  quicktxt(e){
  let {word} = e.currentTarget.dataset,{detail} = this.data
  detail+=word+';'
  that.setData({detail})

  },
  industrychange(e){
    that.setData({industry_choseindex:e.detail.value})
  },
  //工种
  changeType(e) {
    console.log(e, 'eeeeeee');
    let { worktypelist, worktype, worksubtype, worktypename, worksubtypename } = that.data, { value } = e.detail
    worktype = worktypelist[0][value[0]].id
    worksubtype = worktypelist[1][value[1]].id 
    worktypename = worktypelist[0][value[0]].name
    worksubtypename = worktypelist[1][value[1]].name
    that.setData({ worktypename, worktype, worksubtype, worksubtypename })
  },
  TypeColumnChange(e) {
    let { column, value } = e.detail, { worktypelist, subtypeList, primarytypeList } = that.data, list = [], newslist = [{ name: '全部', id: 0 }]
    if (column == 0) {
      subtypeList.forEach(x => {
        if (primarytypeList[value].id == x.cid) {
          list.push(x)
        }
      })
      list = newslist.concat(list)
      worktypelist[1] = list
    }
    that.setData({ worktypelist })
  },
  inputtap(e) {
    let { prop } = e.currentTarget.dataset
    that.setData({ [prop]: e.detail.value })
  },
  gettop_chose(e){
    that.setData({gettopindex:e.currentTarget.dataset.i})
  },
  changeRegion(e){
    console.log(e,'eeeee');
    that.setData({region:e.detail.value,province:e.detail.value[0],city:e.detail.value[1],district:e.detail.value[2]})
  },
  previewimg(e){
    let {src} = e.currentTarget.dataset;
    wx.previewImage({
      urls:[src]
    })
  },
  delimg(e){
    let { index } = e.currentTarget.dataset, { imgurl } = this.data;
    imgurl.splice(index, 1);
    this.setData({imgurl }) 
    // wx.showModal({
    //   title: '提示',
    //   content: '确定要删除吗？', 
    //   success:res=>{
    //     if(res.confirm){
        
    //     }
    //   }
    // })
  },

  提交(gettopindex,data){
    console.log('提交',data);
    if(gettopindex>0){
      //需要支付
      //1先支付
      api.addRecruit(data,res=>{
        wx.hideLoading()
        if(res.data.issuccess==1){ 
          wx.showModal({ 
            title:"提示",
            content:'提交成功',
            showCancel:!1,
            success(res){
              if(res.confirm){
                wx.navigateBack()
              }
            }
          })
        }else{
          util.nocancelmodal(res.data.msg)
        }
      })

  }else{
    api.addRecruit(data,res=>{
      wx.hideLoading()
      if(res.data.issuccess==1){
        wx.showModal({ 
          title:"提示",
          content:'提交成功',
          showCancel:!1,
          success(res){
            if(res.confirm){
              wx.navigateBack()
            }
          }
        })
      }else{
        util.nocancelmodal(res.data.msg)
      }
    })
  }


  },

  submit(){
    let {industry_choseindex,district,title,salary,detail,contacts,phone,recruitsnum,gettopindex,imgurl} = that.data,err=''
    if(title=='') err='请输入招工标题'
    if(industry_choseindex==0) err='请选择行业'
    if(salary==''||isNaN(Number(recruitsnum))||Number(recruitsnum)<1) err='请输入薪资'
    if(phone.length!=11) err='请输入正确的电话号码'
    if(district=='') err='请输入地址'
    if(phone=='') err='请输入电话号码'
    if(contacts=='') err='请输入联系人'
    if(recruitsnum==''||isNaN(Number(recruitsnum))||Number(recruitsnum)<1) err='请输入招聘人数'
    if(detail=='') err='请输入招工详情'
    if(err.length>0){
      wx.showToast({
        title: err,
        icon:'none'
      })
      return
    }
    wx.showLoading({
      title:'提交中...'
    })
    let data={rtype:that.data.industrylist[industry_choseindex],district,title,salary,detail,contacts,phone,recruitsnum} 
    if(imgurl.length>0){
      console.log('有照片上传');
      //有照片上传
 api.uploadimg({path:imgurl},{
         success:res=>{
           console.log(res,'success');
          data['imgurls'] = res.join(',')
          that.提交(gettopindex,data)
         }
        })
    }else{
      console.log('无照片上传');
      that.提交(gettopindex,data)

    }
    

   
   

   

    console.log(data,'datadata');
    return;
    api.addRecruit(data,res=>{
      wx.hideLoading()
      if(res.data.issuccess==1){
        wx.showModal({
          title:"提示",
          content:'提交成功',
          showCancel:!1,
          success(res){
            if(res.confirm){
              wx.navigateBack()
            }
          }
        })
      }else{
        wx.showModal({
          title:"提示",
          content:res.data.msg,
          showCancel:!1,
          success(res){
            if(res.confirm){
            }
          }
        })
      }
    })
  },

  uploadimghere(list,succ){
    console.log(list,'uploadimghere--list');
    api.uploadimg({ url: 'https://techcherry.cn/zghapi/users/mp_uploadimg', path:list }, {
        success: resarr => {
          wx.hideLoading()
          console.log(resarr,'resarr');
         // data['picpath'] = resarr
          succ&&succ(resarr)
         // that.postdata(data)

          // target['imgurl'] = oldarr.concat(resarr);
          // that.confirmsubmit(target)
        }
      })
  },

  addimg(e){
    let { imgurl } = this.data;
    wx.chooseImage({
      count: 2 -imgurl.length,
      sizeType:'compressed',
      success: function(res) {
        imgurl = imgurl.concat(res.tempFilePaths) 

        // api.uploadimg({path:imgurl},{
        //  success:res=>{
        //    console.log(res,'success');
        //  }
        // })

        that.setData({ imgurl})

      },
    })
  },
  toguide(){
    wx.navigateTo({
      url: '../useGuide/useGuide',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})